import store from './index'
import request from '@/utils/drawutils/request'

export default {
    state: {

    },
    mutations: {
        //indexid 对象的序号   fValue 目标值
        //对不同对象的不同属性进行实时处理
        //array是返回集合的数据点数组
        setValue( {componentData} , { indexid, objid,  array }  ){
            // console.log( "parm ", componentData,  indexid, array )
            // fValue , typeof fValue
            if( indexid >= componentData.length )
                return

            //判断对象编号是否一致
            if ( componentData[indexid].id != objid ) {
                console.log( "objid isn't equal", componentData[indexid].id , objid  )
                return 
            }

            //矩形刷新  
            if( array.length < 1 ) return       //至少返回一个数点值
            
            var fValue = array[0].value.value   //前一个value返回一个json对象，后一个value是一个键名，返回一个值

            if( componentData[indexid].component === 'RectShape'  || componentData[indexid].component === 'CircleShape' ){  //componentData[indexid].hasOwnProperty("hg3000")
                let  obj = componentData[indexid].hg3000
                // console.log( "obj value: ", fValue,  typeof fValue, obj.fValue, isNaN(fValue) )
                if( isNaN(fValue) )  {
                    componentData[indexid].propValue =  fValue
                    return   //对于一个非数字
                }
                // console.log( "freshtype: ", obj.freshtype, typeof obj.freshtype  )
                // console.log( "cond: ", !obj.bInit, parseFloat( fValue ) != obj.fValue )

                if( !obj.bInit || parseFloat( fValue ) != obj.fValue ){   //尚未初始化
                    obj.fValue = parseFloat( fValue )
                    if( obj.freshtype == 0 ){   //刷新底色
                        // console.log(  "fvalue: " ,fValue, fValue == 0 )
                        if( fValue == 0 ){
                            componentData[indexid].style.backgroundColor = obj.color0
                            // console.log( "color0", obj.color0 )     
                        }
                        else{
                            componentData[indexid].style.backgroundColor = obj.color1
                            // console.log( "color1", obj.color1 )
                        }
                    }

                    if( obj.freshtype == 1 ){  //数字刷新
                        // console.log( "set digit ")
                        componentData[indexid].propValue = obj.fValue.toFixed( obj.digitnum )
                    }
                    obj.bInit = true
                }
            }
            //图像刷新
            if(  componentData[indexid].component === 'WinSvg' ){
                // console.log( "fresh winsvg" )
                let  obj = componentData[indexid].hg3000

                if(  !obj.bInit || parseFloat(fValue) != obj.fValue ){
                    obj.fValue = parseFloat(fValue)
                    let picmap = componentData[indexid].propValue.picMap
                    // console.log( "picmap", picmap )
                    for( var i=0; i<picmap.length; i++){
                        console.log( fValue, picmap[i].value )
                        if( picmap[i].value == obj.fValue ){
                            componentData[indexid].propValue.picPath = picmap[i].picpath    
                            // console.log( picmap[i].picpath )
                        }
                    }
                    obj.bInit =  true
                }
            }

            //表计刷新
            if(  componentData[indexid].component === 'VMeter' ){
                let  obj = componentData[indexid].hg3000

                if(  !obj.bInit || parseFloat(fValue) != obj.fValue ){
                    obj.fValue = parseFloat(fValue)
                    
                    componentData[indexid].propValue.option.series[0].data[0].value = obj.fValue
                    obj.bInit =  true
                }
            }

            //棒图刷新
            if(  componentData[indexid].component === 'VChart' ){
                if( array.length < 1 ) return       //至少返回一个数点值
                let  obj = componentData[indexid].hg3000
                var fValue , fValue1
                for( var i=0; i<array.length; i++){
                    
                    fValue1 = Number( array[i].value.value )
                    fValue = fValue1.toFixed( obj.digitnum )
                    // console.log( fValue )
                    let dataarray = componentData[indexid].propValue.option.series.data
                    if( i < dataarray.length )
                        dataarray[i] = fValue 
                }
            }

            //表格刷新
            if(  componentData[indexid].component === 'VTable' ){
                if( array.length < 1 ) return       //至少返回一个数点值
                let  obj = componentData[indexid].hg3000
                let fValue , fValue1

                let index = 0
                var dataarray = componentData[indexid].propValue.data
                loop1: for( let i=0; i<dataarray.length; i++){
                    var row = dataarray[i]
                    for( let j=0; j<row.length; j++ ){
                        fValue1 = Number( array[index].value.value )
                        fValue = fValue1.toFixed( obj.digitnum )
                        row[j] = fValue
                        index ++
                        if( index >= array.length ){
                            break loop1
                        }
                    }
                }

            }  // end if VTable

            //刷新VRealCurve
            if(  componentData[indexid].component === 'VRealCurve' ){
                let  realdat = []
                for( let i=0; i<array.length; i++){
                    realdat.push( array[i].value.value )
                }
                componentData[indexid].propValue.rtdata = realdat
                componentData[indexid].propValue.rtDataIns ++ 
                
            }

        },  // end setValue

        //清除对象的init标志
        //2024.12.20 去除实时曲线中的数据避免图形路跃
        clearInit( {componentData}  ){
            for (let i = 0; i < componentData.length; i++ ){ 
                // console.log( i, componentData[i] )
                if( componentData[i].hasOwnProperty("hg3000")  ) //组合对象未考虑hg3000的属性
                    componentData[i].hg3000.bInit = false 
                // console.log( "clear init " )
            }

        },


    },        

}
